package cn.ray_land.dlprint.main;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import cn.ray_land.dlprint.R;
import cn.ray_land.dlprint.data.SliceData;
import cn.ray_land.dlprint.print.PrintActivity;

public class MainActivity extends AppCompatActivity implements MainContract.View{

    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;
    private SliceDataAdapter adapter;
    private MainContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        init();
        mPresenter = new MainPresenter(this);
        mPresenter.loadSliceDatas();
    }

    private void init() {
        adapter = new SliceDataAdapter();
        recyclerView = (RecyclerView) findViewById(R.id.rv_recycler_view);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_refreshView);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.loadSliceDatas();
            }
        });
    }

    @Override
    public void showSliceDatas(List<SliceData> datas) {
        adapter.setDatas(datas);
    }

    @Override
    public void showLoadingIndicator(boolean show) {
        refreshLayout.setRefreshing(show);
    }

    class SliceDataAdapter extends RecyclerView.Adapter<SliceDataAdapter.ViewHolder>{

        List<SliceData> datas = new ArrayList<>(0);

        public void setDatas(List<SliceData> sliceDatas){
            this.datas = sliceDatas;
            notifyDataSetChanged();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_slice_data, null));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.title.setText(datas.get(position).getSliceFoler().getName());
            holder.title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PrintActivity.class);
                    intent.putExtra("sliceData", datas.get(position));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            TextView title;
            public ViewHolder(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.tv_title);
            }
        }
    }

}
